package com.wangyg.juc.consumer;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by wangyg6 on 2018/7/4.
 * 买面包的
 */
public class BreadBuyer extends Thread {
	BlockingQueue bq;
	AtomicInteger i = new AtomicInteger();

	public BreadBuyer(String name) {
		super(name);
	}

	public BreadBuyer setBq(BlockingQueue bq) {
		this.bq = bq;
		return this;
	}

	public void run() {
		synchronized (bq) {

			while (bq.isEmpty()) {
				try {
					bq.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
//			int count = i.incrementAndGet();
			String poll = (String)bq.poll();
			System.out.println(Thread.currentThread().getName() + "消费了第" + poll + "个面包");
			bq.notifyAll();

		}
	}
}
